<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #result {
            width: 300px;
            height: 300px;
            border: 1px solid lightskyblue;
        }
    </style>
</head>

<body>
    <div>
        <button>发送请求</button>
        <div id="result"></div>
    </div>
    <script>
        const result = document.getElementById('result')
        const btn = document.querySelectorAll('button')[0]
        btn.onclick = function () {
            console.log("jdjsfaklsdjalfksa")
            const xml = new XMLHttpRequest()
            // 自动转换需要设置响应体的格式
            xml.responseType = 'json'
            xml.open('get', 'http://127.0.0.1:8000/server', true)
            xml.send()
            xml.onreadystatechange = function () {
                if (xml.readyState === 4) {
                    if (xml.status == 200 && xml.status < 300) {
                        // result.innerHTML = xml.response

                        // let data = JSON.parse(xml.response)
                        // console.log(data)
                        // result.innerHTML = data.name

                        // 自动转换
                        console.log(xml.response)
                        result.innerHTML = xml.response.name
                    } else {

                    }
                } else {

                }
            }
        }
    </script>
</body>

</html>